package com.javasm.mingming.login.entity;

import com.javasm.mingming.adminuser.entity.AdminAuthority;
import com.javasm.mingming.adminuser.entity.AdminMenu;
import com.javasm.mingming.adminuser.entity.AdminRole;
import com.javasm.mingming.adminuser.entity.AdminUser;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author javasm
 * @version 0.1
 * @className LoginUserDetails
 * @descriptioin:
 * @date 2025/1/11 15:05
 * @since jdk11
 */
@NoArgsConstructor
@Getter
public class LoginUserDetails implements UserDetails {

    private AdminUser adminUser;

    public LoginUserDetails(AdminUser adminUser) {
        this.adminUser = adminUser;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //权限列表,暂时不考虑,所有的连接都没有权限
        //从数据库,已经查询出 角色信息 和菜单信息 到adminUser
        //角色授权,权限标识授权
        //new 返回的 权限列表
        List<GrantedAuthority> authorityList = new ArrayList<>();
        //查询出 角色的信息
        AdminRole adminRole = adminUser.getAdminRole();
        if (adminRole != null){
            SimpleGrantedAuthority authority = new SimpleGrantedAuthority("ROLE_" + adminRole.getCode());
            authorityList.add(authority);
        }
        //权限列表
        List<AdminAuthority> adminUserAuthorityList = adminUser.getAuthorityList();
        if (adminUserAuthorityList!=null){
            adminUserAuthorityList.forEach(authority->{
                authorityList.add(new SimpleGrantedAuthority(authority.getCode()));
            });
        }
        return authorityList;
    }

    @Override
    public String getPassword() {
        //返回数据库查询出来的密码
        return adminUser.getPassword();
    }

    @Override
    public String getUsername() {
        //返回数据库查询出来的用户名
        return adminUser.getUsername();
    }
    ////注意!!!!!!!以下几个方法,返回值 必须是true
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        //=1有效 =0无效
        Integer isvalid = adminUser.getIsvalid();
        return (isvalid != null && isvalid == 1);
    }

    public boolean checkMenu(String url) {
        if (adminUser!=null){
            //获取 菜单信息
            AdminRole adminRole = adminUser.getAdminRole();
            if (adminRole != null){
                //菜单列表,但是 菜单列表 是父子关系,现在需要把childList 也拿出来
                List<AdminMenu> menuList = adminRole.getMenuList();
                //menuList→stream→获取childList的流→url流
                Stream<String> childMenuUrlList = menuList.stream()
                        .map(AdminMenu::getChildList)
                        .flatMap(Collection::stream)
                        .map(AdminMenu::getUrl);
                //一级菜单url
                Stream<String> firstMenuUrlList = menuList.stream().map(AdminMenu::getUrl);
                //两个流合并到一起,重新组装
                List<String> urlList = Stream.concat(childMenuUrlList, firstMenuUrlList)
                        .collect(Collectors.toList());
                return urlList.contains(url);
            }
        }

        return false;
    }
}
